package com.qfedu.aunt.commons.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.aunt.commons.exception.BusinessException;
import com.qfedu.aunt.commons.mapper.AgentMapper;
import com.qfedu.aunt.commons.mapper.AuntMapper;
import com.qfedu.aunt.commons.pojo.Agent;
import com.qfedu.aunt.commons.pojo.Aunt;
import com.qfedu.aunt.commons.pojo.EmployerCollection;
import com.qfedu.aunt.commons.mapper.EmployerCollectionMapper;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.service.EmployerCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 雇主收藏表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2022-10-26
 */
@Service
public class EmployerCollectionServiceImpl extends ServiceImpl<EmployerCollectionMapper, EmployerCollection> implements EmployerCollectionService {

    @Autowired
    private EmployerCollectionMapper employerCollectionMapper;

    @Autowired
    private AgentMapper agentMapper;

    @Autowired
    private AuntMapper auntMapper;

    @Override
    public Map<String,Object> findCollectionByEmployerId(String employerId) {
        if(StringUtils.isEmpty(employerId)){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }

        QueryWrapper<EmployerCollection> employerCollectionQueryWrapper = new QueryWrapper<>();
        employerCollectionQueryWrapper.eq("employer_id",employerId);

        //执行到这里 说明参数没有问题
        //这里面查询出来既有收藏的经纪人 还有收藏的阿姨
        List<EmployerCollection> employerCollections = employerCollectionMapper.selectList(employerCollectionQueryWrapper);

        //这个是专门用来放经纪人的id的集合
        List<String> agentIds=new ArrayList<>();

        //这个是专门用来放阿姨的id的集合
        List<String> auntIds=new ArrayList<>();

        //下面要拆分出收藏的是经纪人还是阿姨
        for (EmployerCollection e:employerCollections) {
            if(e.getAgentId()!=null){
                //说明收藏是是经纪人
                agentIds.add(e.getAgentId());
            }else{
                //说明收藏是阿姨
                auntIds.add(e.getAuntId());
            }
        }
        //接下来：通过经纪人的集合查询经纪人的信息
        List<Agent> agents = agentMapper.selectBatchIds(agentIds);
        List<Aunt> aunts = auntMapper.selectBatchIds(auntIds);

        Map<String,Object> maps=new HashMap<>();
        maps.put("aunt",aunts);
        maps.put("agent",agents);

        return maps;
    }
}
